@tool
@icon("icons/compound_state.svg")
class_name RandomState
extends State

@export var probabilities:Array[int]
var cur_state:State
var sub_states:Array[State] = []
var sub_state_dict:Dictionary = {}

func state_init(fsm:FSM):
	super.state_init(fsm)
	for child in get_children():
		if child is State:
			sub_states.append(child)
			sub_state_dict[child.state_name] = child
			child.state_init(fsm)
func on_enter(from_state:State):
	super.on_enter(from_state)
func on_update(delta:float):
	super.on_update(delta)
	if super.get_state_status() == GameEnum.StateStatus.Waiting:
		return
	cur_state.on_update(delta)
func on_physics_update(delta:float):
	super.on_physics_update(delta)
	if super.get_state_status() == GameEnum.StateStatus.Waiting:
		return
	cur_state.on_physics_update(delta)
func on_exit(to_state:State):
	cur_state.on_exit(to_state)
	cur_state = null
	super.on_exit(to_state)
func get_state_status() -> GameEnum.StateStatus:
	if cur_state_status == GameEnum.StateStatus.Closed and sub_states.any(func (s):return s.get_state_status() == GameEnum.StateStatus.Ready):
		cur_state_status = GameEnum.StateStatus.Ready
	return cur_state_status
func translate_state(st_state:State,to_state:State,st_state_path:Array[State],to_state_path:Array[State]):
	var st_state_temp:State = st_state_path.pop_back()
	var to_state_temp:State = to_state_path.pop_back()
	if not st_state_temp:
		if to_state_temp:
			on_enter(st_state)
			cur_state = to_state_temp
			cur_state.translate_state(st_state,to_state,st_state_path,to_state_path)
		else:
			on_enter(st_state)
			var temp_states = []
			var temp_probabilities = []
			for i in range(sub_states.size()):
				if sub_states[i].get_state_status() == GameEnum.StateStatus.Ready:
					temp_states.append(sub_states[i])
					temp_probabilities.append(probabilities[i])
			cur_state = Util.random_get_count_by_probability(temp_states,temp_probabilities,1,false)
			cur_state.translate_state(st_state,to_state,st_state_path,to_state_path)
	elif st_state_temp:
		if to_state_temp:
			if st_state_temp.state_name == to_state_temp.state_name:
				to_state_temp.translate_state(st_state,to_state,st_state_path,to_state_path)
			else:
				cur_state.on_exit(to_state)
				cur_state = to_state_temp
				cur_state.translate_state(st_state,to_state,st_state_path,to_state_path)
		else:
			if sub_states.has(st_state_temp):
				cur_state.on_exit(to_state)
			var temp_states = []
			var temp_probabilities = []
			for i in range(sub_states.size()):
				if sub_states[i].get_state_status() == GameEnum.StateStatus.Ready:
					temp_states.append(sub_states[i])
					temp_probabilities.append(probabilities[i])
			cur_state = Util.random_get_count_by_probability(temp_states,temp_probabilities,1,false)
			cur_state.translate_state(st_state,to_state,st_state_path,to_state_path)
func _get_configuration_warnings() -> PackedStringArray:
	var warnings = super._get_configuration_warnings()
	var child_count = 0
	for child in get_children():
		if child is State:
			child_count += 1
	if child_count < 1:
		warnings.append("RandomState" + "至少需要1个State作为初始State")
	elif child_count < 2:
		warnings.append("RandomState" + "至少需要两个State才有效，如果只需要一个State，建议不使用CompoundState")
	return warnings
